#============================================================
# Create Time:			2021-11-02 08:35:38
# Last modify:			2021-11-02 03:33:26
# Writer:				Wenhao	wh.wenhao@foxmail.com
# File Name:			client.py
# File Type:			PY Source File
# Tool:					Mac -- vim & python
# Information:			
#============================================================

import os
import sys
import time
import struct
import socket
import argparse

from protocal import send_data, recv_data

def build_socket(host='10.0.2.2', port=5678):
    tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        tcp_socket.connect((host, port))
    except Exception:
        print('Server not found or not open!')
        sys.exit()
    return tcp_socket

def build_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--file', '-f', type=str, required=True, help='The file to be analysed.')
    args = parser.parse_args()
    return args

if __name__ == '__main__':
    args = build_args()
    assert os.path.exists(args.file), 'The file you are analysing doesn\'t exist!'
    tcp_socket = build_socket()

    for line in open(args.file, 'r', encoding='utf-8'):
        time.sleep(0.1)
        if line.startswith('$GPGGA'):
            send_data(tcp_socket, line.strip())
        elif line.startswith('$GPRMC'):
            send_data(tcp_socket, line.strip())
            data = recv_data(tcp_socket)
            print(data)
    send_data(tcp_socket, '')
    tcp_socket.close()
